package com.symbio.ishenapi.order.controller;

import com.jfinal.kit.StrKit;
import com.symbio.ishenapi.common.controller.BaseController;
import com.symbio.ishenapi.order.bean.OrderCountRequest;
import com.symbio.ishenapi.order.model.TmOrderCount;
import com.symbio.ishenapi.order.model.TmOrderDictionary;
import com.symbio.ishenapi.order.model.TmOrderInvoice;
import com.symbio.ishenapi.order.model.TmServiceOrder;
import io.jboot.web.controller.annotation.RequestMapping;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author guoshunyou
 *
 */
@RequestMapping("/TmInvoiceOrderController")
public class TmInvoiceOrderController extends BaseController {

    /**
     * @author guoshunyou
     * 根据userId、orderType查询指定服务次数
     */
    public void getServiceCount(){
        TmOrderCount toc=(TmOrderCount)getReqBodyObject(TmOrderCount.class);

        TmOrderCount co=TmOrderCount.dao.getServiceCount(toc);
        if(co==null){
            renderJsonError(40003, "该患者此服务");
        }else{
            renderJsonRet(co);
        }
    }

    /**
     * wy:后台
     * 根据userId、orderType,insuranceId查询指定服务次数
     */
    public void findServiceCount(){
        TmOrderCount toc=(TmOrderCount)getReqBodyObject(TmOrderCount.class);

        List<TmOrderCount> co=TmOrderCount.dao.findServiceCount(toc);
        if(co==null && co.size() > 0){
            renderJsonError(40003, "该患者此服务");
        }else{
            renderJsonRet(co);
        }
    }

    /**
     * @author guoshunyou
     * 根据userid  doctorid查询具体的指定医生的服务及次数（参数可控制）
     */
    public void getServiceDocCount(){
        TmOrderCount toc=(TmOrderCount)getReqBodyObject(TmOrderCount.class);
        List<TmOrderCount> lsitt= TmOrderCount.dao.getListServiceCount(toc);
        List listAll=new ArrayList<>();
        Map map1=new HashMap();
        Map map2=new HashMap();
        Map map3=new HashMap();
        Map map4=new HashMap();

        List list1=new ArrayList();
        List list2=new ArrayList();
        List list3=new ArrayList();
        List list4=new ArrayList();
       if(lsitt==null){
           renderJsonError(40003, "该患者此服务");
       }else{
           for (int i=0;i<lsitt.size();i++){
               TmOrderCount tm= lsitt.get(i);
               if("180919007".equals(tm.getRoleCode())){
                   if("180919001".equals(tm.getOrderType())){
                       Map map1x=new HashMap<>();
                       map1x.put("serviceType","180919001");
                       map1x.put("serviceName","图文咨询");
                       map1x.put("serviceCount",tm.getCount());
                       list1.add(map1x);
                   }else if("180919002".equals(tm.getOrderType())){
                       Map map2x=new HashMap<>();
                       map2x.put("serviceType","180919002");
                       map2x.put("serviceName","电话咨询");
                       map2x.put("serviceCount",tm.getCount());
                       list1.add(map2x);
                   }else if("180919003".equals(tm.getOrderType())){
                       Map map3x=new HashMap<>();
                       map3x.put("serviceType","180919003");
                       map3x.put("serviceName","视频咨询");
                       map3x.put("serviceCount",tm.getCount());
                       list1.add(map3x);
                   }else if("180919004".equals(tm.getOrderType())){
                       Map map4x=new HashMap<>();
                       map4x.put("serviceType","180919004");
                       map4x.put("serviceName","快速开药");
                       map4x.put("serviceCount",tm.getCount());
                       list1.add(map4x);
                   }else if("180919005".equals(tm.getOrderType())){
                       Map map5x=new HashMap<>();
                       map5x.put("serviceType","180919005");
                       map5x.put("serviceName","私人医生");
                       map5x.put("serviceCount",tm.getCount());
                       list1.add(map5x);
                   }



               }else if("180919008".equals(tm.getRoleCode())){

                   if("180919001".equals(tm.getOrderType())){
                       Map map1x=new HashMap<>();
                       map1x.put("serviceType","180919001");
                       map1x.put("serviceName","图文咨询");
                       map1x.put("serviceCount",tm.getCount());
                       list2.add(map1x);
                   }else if("180919002".equals(tm.getOrderType())){
                       Map map2x=new HashMap<>();
                       map2x.put("serviceType","180919002");
                       map2x.put("serviceName","电话咨询");
                       map2x.put("serviceCount",tm.getCount());
                       list2.add(map2x);
                   }else if("180919003".equals(tm.getOrderType())){
                       Map map3x=new HashMap<>();
                       map3x.put("serviceType","180919003");
                       map3x.put("serviceName","视频咨询");
                       map3x.put("serviceCount",tm.getCount());
                       list2.add(map3x);
                   }else if("180919004".equals(tm.getOrderType())){
                       Map map4x=new HashMap<>();
                       map4x.put("serviceType","180919004");
                       map4x.put("serviceName","快速开药");
                       map4x.put("serviceCount",tm.getCount());
                       list2.add(map4x);
                   }else if("180919005".equals(tm.getOrderType())){
                       Map map5x=new HashMap<>();
                       map5x.put("serviceType","180919005");
                       map5x.put("serviceName","私人医生");
                       map5x.put("serviceCount",tm.getCount());
                       list2.add(map5x);
                   }



               }else if("180919009".equals(tm.getRoleCode())){

                   if("180919001".equals(tm.getOrderType())){
                       Map map1x=new HashMap<>();
                       map1x.put("serviceType","180919001");
                       map1x.put("serviceName","图文咨询");
                       map1x.put("serviceCount",tm.getCount());
                       list3.add(map1x);
                   }else if("180919002".equals(tm.getOrderType())){
                       Map map2x=new HashMap<>();
                       map2x.put("serviceType","180919002");
                       map2x.put("serviceName","电话咨询");
                       map2x.put("serviceCount",tm.getCount());
                       list3.add(map2x);
                   }else if("180919003".equals(tm.getOrderType())){
                       Map map3x=new HashMap<>();
                       map3x.put("serviceType","180919003");
                       map3x.put("serviceName","视频咨询");
                       map3x.put("serviceCount",tm.getCount());
                       list3.add(map3x);
                   }else if("180919004".equals(tm.getOrderType())){
                       Map map4x=new HashMap<>();
                       map4x.put("serviceType","180919004");
                       map4x.put("serviceName","快速开药");
                       map4x.put("serviceCount",tm.getCount());
                       list3.add(map4x);
                   }else if("180919005".equals(tm.getOrderType())){
                       Map map5x=new HashMap<>();
                       map5x.put("serviceType","180919005");
                       map5x.put("serviceName","私人医生");
                       map5x.put("serviceCount",tm.getCount());
                       list3.add(map5x);
                   }



               }else if("180919010".equals(tm.getRoleCode())){

                   if("180919001".equals(tm.getOrderType())){
                       Map map1x=new HashMap<>();
                       map1x.put("serviceType","180919001");
                       map1x.put("serviceName","图文咨询");
                       map1x.put("serviceCount",tm.getCount());
                       list4.add(map1x);
                   }else if("180919002".equals(tm.getOrderType())){
                       Map map2x=new HashMap<>();
                       map2x.put("serviceType","180919002");
                       map2x.put("serviceNname","电话咨询");
                       map2x.put("serviceCount",tm.getCount());
                       list4.add(map2x);
                   }else if("180919003".equals(tm.getOrderType())){
                       Map map3x=new HashMap<>();
                       map3x.put("serviceType","180919003");
                       map3x.put("serviceName","视频咨询");
                       map3x.put("serviceCount",tm.getCount());
                       list4.add(map3x);
                   }else if("180919004".equals(tm.getOrderType())){
                       Map map4x=new HashMap<>();
                       map4x.put("serviceType","180919004");
                       map4x.put("serviceName","快速开药");
                       map4x.put("serviceCount",tm.getCount());
                       list4.add(map4x);
                   }else if("180919005".equals(tm.getOrderType())){
                       Map map5x=new HashMap<>();
                       map5x.put("serviceType","180919005");
                       map5x.put("serviceName","私人医生");
                       map5x.put("serviceCount",tm.getCount());
                       list4.add(map5x);
                   }


               }
           }

           map1.put("role","180919007");
           map1.put("roleName","看医生");
           map1.put("count",list1);
           listAll.add(map1);


           map2.put("role","180919008");
           map2.put("roleName","找专家");
           map2.put("count",list2);
           listAll.add(map2);

           map3.put("role","180919009");
           map3.put("roleName","问营养师");
           map3.put("count",list3);
           listAll.add(map3);

           map4.put("role","180919010");
           map4.put("roleName","询药师");
           map4.put("count",list4);
           listAll.add(map4);


           System.out.println("长度："+listAll.size());
           renderJsonRet(listAll);
       }
    }


    /**
     * @author guoshunyou
     * 开发票接口（保存发票订单信息、并同时更新订单表相对应的订单发票状态）
     */
    public void updateOrder(){
        TmOrderInvoice idi=(TmOrderInvoice)getReqBodyObject(TmOrderInvoice.class);
        if(idi.getEmail()==null || "".equals(idi.getEmail())){
            renderJsonError(40003, "邮箱地址");
        }else if(idi.getInvoiceCon()==null || "".equals(idi.getInvoiceCon())){
            renderJsonError(40003, "发票人信息");
        }else if(idi.getOrderId()==null || "".equals(idi.getOrderId())){
            renderJsonError(40003, "订单编号id");
        }else{
            //判断所选开票订单id是否属于该客户userId
            String  countUserId=TmServiceOrder.dao.getCountUserById(idi.getOrderId(),idi.getUserId());
            int coo=Integer.valueOf(countUserId);
            String idStr=idi.getOrderId();
            String[] idStrarray=idStr.split(",");
            System.out.println("userid次数："+coo+"  订单id次数："+idStrarray.length);
            if(coo!=idStrarray.length){
                renderJsonError(40010);
            }else{

                //进行字符串判断是否传的是订单id集合
                String str=new String(idi.getOrderId());
                String lastId="";
                if(str.indexOf(",")>=0){
                    String[] arraystr=str.split(",");
                    lastId=arraystr[0];
                }else{
                    lastId=str;
                }
                //查询该订单是否符合可以开票的状态
                boolean bfl=TmServiceOrder.dao.getFlagById(lastId);
                if(bfl){
                    //取出所有需要开票订单总金额
                    String tsor=TmServiceOrder.dao.getSUMPrice(idi.getOrderId());
                    System.out.println("订单总金额：-- "+tsor);
                    ///调用开票接口 true false
                    if(true){
                        //发票订单表  开票状态改为1 开票成功
                        idi.setInvoiceStatus("1");
                        //追加发票总金额
                        idi.setInvoiceSumPrice(tsor);
                        idi.save();
                        //更新订单表  开票状态位 1  已开票
                        int orderboo=TmServiceOrder.dao.updateServiceOrder(idi.getOrderId(),"1");
                        if(orderboo>=1){
                            renderJsonError(40001,"发票信息");
                        }else{
                            renderJsonError(40005,"订单发票状态");
                        }
                    }else{
                        //发票订单表  开票状态改为 0 开票失败
                        idi.setInvoiceStatus("0");
                        idi.setInvoiceSumPrice(tsor);
                        idi.save();
                        //更新订单表  开票状态位  0  未开票
                        int orderboo=TmServiceOrder.dao.updateServiceOrder(idi.getOrderId(),"0");
                        renderJsonError(40009,"开票");
                    }
                }else{
                    renderJsonError(40008,"该订单开票");
                }
            }
        }
    }

    /**
     * @author guoshunyou
     * 根据指定id修改订单的开票状态为开票中
     */
    public void updateFlag(){
        TmOrderInvoice idi=(TmOrderInvoice)getReqBodyObject(TmOrderInvoice.class);
        int in=TmServiceOrder.dao.updateServiceOrder(idi.getOrderId(),"0");
        if(in>=1){
            renderJsonError(40004,"订单发票状态");
        }else{
            renderJsonError(40005,"订单发票状态");
        }
    }

    /**
     *
     *
     * @Author GuoShunYou
     * @Description 根据userid查询  是否需要提示 快速开药弹窗
     * @date 下午 5:21   2018/10/26
     * @return void
     */
    public void getordercount(){
        TmOrderCount idi=(TmOrderCount)getReqBodyObject(TmOrderCount.class);
        System.out.println(idi.getUserId());
        List<String> lsit=TmOrderCount.dao.getcountbyservicetype(idi);
        System.out.println(lsit);
        String count=lsit.get(0);
        String count1=lsit.get(1);
        String count2=lsit.get(2);
        String count3=lsit.get(3);
        int a=0;
        int a1=0;
        int a2=0;
        int a3=0;
        System.out.println(count +"  "+count1+"  "+count2+"  "+count3);
        if(!StrKit.isBlank(count)){
            a=Integer.valueOf(count);
        }else if(!StrKit.isBlank(count1)){
            a1=Integer.valueOf(count1);
        }else if(!StrKit.isBlank(count2)){
            a2=Integer.valueOf(count2);
        }else if(!StrKit.isBlank(count3)){
            a3=Integer.valueOf(count3);
        }

        if(a>0 || a1>0 || a2>0 || a3>0){
            renderJsonError(40015,"快速开药服务");
            return;
        }else{
            renderJsonError(40016,"快速开药服务");
            return;
        }

    }


    /**
     *
     *
     * @Author GuoShunYou
     * @Description 初始化服务次数
     * @date 下午 5:41   2018/10/27
     * @return void
     */
    public void saveOrderCount(){
        TmOrderCount toc = (TmOrderCount) getReqBodyObject(TmOrderCount.class);
        try {
            List<TmOrderDictionary> list = TmOrderDictionary.dao.saveOrderCount();
            int count = 0;
            toc.setCount(5);
            for (int i=0;i<list.size();i++){
                toc.setOrderType(list.get(i).getStandCode());
                toc.setId(null);
                if (toc.save()){
                    count++;
                }
            }
            if (count == list.size()){
                renderJsonError(40001,"医疗团初始化次数");
                return;
            }else {
                renderJsonError(40002,"医疗团初始化次数");
                return;
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    /**
     *
     *
     * @Author GuoShunYou
     * @Description 根据userID、doctorid、服务类型、服务次数、角色类型roleCode添加指定服务记录
     * @date 上午 3:32   2018/10/29
     * @return void
     */
    public void addServiceCount(){
        TmOrderCount toc = (TmOrderCount) getReqBodyObject(TmOrderCount.class);
        if(toc.getUserId()==null || "".equals(toc.getUserId())){
            renderJsonError(40003,"userid");
            return;
        }else if(toc.getOrderType()==null || "".equals(toc.getOrderType())){
            renderJsonError(40003,"服务类型编号");
            return;
        }else if((toc.getDoctorId()==null || "".equals(toc.getDoctorId())) && (toc.getInsuranceId()==null || "".equals(toc.getInsuranceId()))){
            renderJsonError(40003,"医生ID或保险团ID");
            return;
        }
        int coo=TmOrderCount.dao.selectOrderCount(toc);
        if(coo==0){

            if(toc.getCount()==null || "".equals(toc.getCount())){
                toc.setCount(1);
            }

            toc.save();
        }else{
            TmOrderCount tmorc=TmOrderCount.dao.selectId(toc);
            toc.setId(tmorc.getId());
            if(toc.getCount()==null || "".equals(toc.getCount())){
                toc.setCount(tmorc.getCount()+1);
            }else {
                toc.setCount(tmorc.getCount()+toc.getCount());
            }
            toc.update();
        }
    }


    /**
     *
     *
     * @Author GuoShunYou
     * @Description 获取保险订单信息  初始化到服务次数表(批量添加服务次数)
     * @date 上午 11:57   2018/11/6
     * @return void
     */
    public void addService(){
        OrderCountRequest toc = (OrderCountRequest) getReqBodyObject(OrderCountRequest.class);
        if(toc.getList()==null || "".equals(toc.getList())){
            renderJsonError(40003,"list");
            return;
        }else if((toc.getDoctorId()==null || "".equals(toc.getDoctorId())) && (toc.getInsuranceId()==null || "".equals(toc.getInsuranceId()))){
            renderJsonError(40003,"doctorId");
            return;
        }else if(toc.getUserId()==null || "".equals(toc.getUserId())){
            renderJsonError(40003,"userId");
        }
        int count=0;
        for(int i=0;i<toc.getList().size();i++){
            TmOrderCount orc=new  TmOrderCount();
            orc.setUserId(toc.getUserId());
            orc.setDoctorId(toc.getDoctorId());
            orc.setInsuranceId(toc.getInsuranceId());
            orc.setOrderType(toc.getList().get(i).getServerType());
            orc.setCount(toc.getList().get(i).getServerCount());
            orc.setRoleCode(toc.getList().get(i).getRoleType());
            boolean b=orc.save();
            if (b){
                count++;
            }
        }
        if(count == toc.getList().size()){
            renderJsonRet(50001);
        }else{
            renderJsonError(50002,"初始化次数");
        }
    }




    /**
     *  
     *
     * @Author GuoShunYou
     * @Description 使用完服务  更新指定服务次数   根据  userid、doctorid、ordertype、rolecode
     * @date 下午 4:25   2018/11/2
     * @return void
     */
    public  void updateCount(){
        TmOrderCount toc = (TmOrderCount) getReqBodyObject(TmOrderCount.class);
        int num=TmOrderCount.dao.updateCount(toc);
        if(num>0){
            renderJsonRet(50001);
        }else{

            renderJsonError(50002,"更新服务次数");
        }

    }

    /**
     * wy:根据患者Ids和医生Id查询已经服务了多少次
     */
    public void findByUserIdsAndDoctorIdSelectCount(){
        String doctorId = getPara("doctorId");
        Map map = new HashMap();
        String userIds = "";
        if (doctorId != null && !"".equals(doctorId)) {
            List<TmOrderCount> count = TmOrderCount.dao.findByUserIdsAndDoctorIdSelectCount(doctorId);
            for (int i = 0; i < count.size(); i++) {
                userIds += count.get(i).getUserId()+"";
                if (i+1 < count.size()){
                    userIds += ",";
                }
            }
            map.put("userIds",userIds);
            map.put("count",count);
            renderJsonRet(map);
        }else {
            renderJsonError(40003,"0");
        }
    }


}
